package com.sws.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sws.Constant;
import com.sws.exception.ParseXmlException;
import com.sws.model.ActivityModel;
import com.sws.model.bean.C2SActivityBatchBean;
import com.sws.model.bean.C2SActivityBean;
import com.sws.model.bean.C2SPageBean;
import com.sws.service.ActivityService;
import com.sws.utils.CUtils;
import com.sws.utils.ActivityRecordParse;

@Controller
public class ActivityController {
	
	private Logger logger = LoggerFactory.getLogger(ActivityController.class);
	private static final String LOCATION_PATH = "C:/ServerUpload/SWSUpload/activity";
	private static final String REQUEST_PATH = "SWSUpload/activity/";
//	private static final String LOCATION_PATH = "E:/serverfie/SWSUpload/activity";
	
	@Autowired
	private ActivityService service;
	
	@ResponseBody
	@RequestMapping("/s/uploadActivity")
	public String uploadCommonActivity(C2SActivityBatchBean activity, HttpServletRequest request) {
		logger.info("=====> 批量上传公共活动");
		JSONObject result = new JSONObject();
		String fileName = "activity_" + CUtils.getDateTime() + "_" + activity.getFile().getOriginalFilename();
		File dir = new File(LOCATION_PATH);
		//判断上传文件的保存目录是否存在
		if (!dir.exists() && !dir.isDirectory()) {
			System.out.println("创建目录");
			dir.mkdirs();
		}
		try {
			File saveFile = new File(LOCATION_PATH, fileName);
			activity.getFile().transferTo(saveFile);
			service.uploadCommonActivity(ActivityRecordParse.xlsParse(LOCATION_PATH+"/"+fileName, 0));
			result.put("status", Constant.INSTANSE.OK);
			result.put("msg", "批量上传成功！");
		} catch (ParseXmlException e) {
			// TODO: handle exception
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.FILE_ERROR);
			result.put("msg", "文件错误，请选择正确文件！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/s/getPersonalActivity")
	public String getPersonalActivity(C2SPageBean pageBean) {
		logger.info("=====> 获取公共活动信息");
		JSONObject result = new JSONObject();
		
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getPersonalActivity(pageBean));
			result.put("msg", "获取公共活动信息成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/s/addActivity")
	public String addActivity(C2SActivityBean bean) {
		logger.info("=====> 上传公共活动信息");
		JSONObject result = new JSONObject();
		
		try {
			ActivityModel model = new ActivityModel(bean);
			service.uploadCommonActivity(model);
			if (model.getIsSuccess() != null && model.getIsSuccess()) {
				result.put("status", Constant.INSTANSE.OK);
				result.put("msg", "上传公共活动信息成功！");
			} else {
				result.put("status", Constant.INSTANSE.FAIL);
				result.put("msg", "上传失败，请检查学号等数据是否正确！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}

}
